/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

package scm;
import java.sql.*;
import javax.swing.*;

/**
 *
 * @author Łukasz Synówka
 */
public class EdytujAkcje extends JFrame
{

    private Connection c = null;
    private int actionId = 0;
    DefaultListModel listModel = new DefaultListModel();

    Akcje parentFrame = null;

    /** Creates new form DodawanieProcedury */
    public EdytujAkcje(Akcje parentFrame, int actionId)
    {
        this.parentFrame = parentFrame;
        this.actionId = actionId;
        initComponents();
        updateFields(actionId);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    private void updateFields(int actionId)
    {
        try
        {
            Class.forName("org.postgresql.Driver");
        }
        catch (ClassNotFoundException cnfe)
        {
            JOptionPane.showMessageDialog(null, cnfe.getMessage());
        }
        try
        {
            c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");
             
            PreparedStatement ps = c.prepareStatement("SELECT name, description, action FROM actions WHERE id = ?");
            ps.setInt(1, actionId);
            ResultSet rs = ps.executeQuery();

            if (rs.next())
            {
                jTextFieldName.setText(rs.getString("name"));
                jTextFieldDescription.setText(rs.getString("description"));
                jTextFieldAction.setText(rs.getString("action"));
            }
            else
            {
                this.setVisible(true);
                this.dispose();
            }
         }
         catch (SQLException se)
         {
             JOptionPane.showMessageDialog(null, se.getMessage());
         }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jButtonSave = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jTextFieldName = new javax.swing.JTextField();
        jTextFieldAction = new javax.swing.JTextField();
        jTextFieldDescription = new javax.swing.JTextField();
        jLabelName = new javax.swing.JLabel();
        jLabelAction = new javax.swing.JLabel();
        jLabelDescription = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N


        jLabelName.setName("jLabelName");
        jLabelName.setText("Nazwa");

        jLabelAction.setName("jLabelName");
        jLabelAction.setText("Akcja");

        jLabelDescription.setName("jLabelDescription");
        jLabelDescription.setText("Opis");

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jButtonSave.setText("Zapisz"); // NOI18N
        jButtonSave.setName("jButtonSave"); // NOI18N
        jButtonSave.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jButtonSaveMouseClicked(evt);
            }
        });

        jButtonCancel.setText("Anuluj"); // NOI18N
        jButtonCancel.setName("jButtonCancel"); // NOI18N
        jButtonCancel.addMouseListener(new java.awt.event.MouseAdapter()
        {
            public void mouseClicked(java.awt.event.MouseEvent evt)
            {
                jButtonCancelMouseClicked(evt);
            }
        });

        jTextFieldName.setText(""); // NOI18N
        jTextFieldName.setName("jTextFieldName"); // NOI18N

        jTextFieldAction.setText(""); // NOI18N
        jTextFieldAction.setName("jTextFieldAction"); // NOI18N

        jTextFieldDescription.setText(""); // NOI18N
        jTextFieldDescription.setName("jTextFieldDescription"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabelName)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabelAction)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTextFieldAction)))
                         .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabelDescription)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTextFieldDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jButtonSave)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelName)
                    .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelAction)
                    .addComponent(jTextFieldAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDescription)
                    .addComponent(jTextFieldDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>


    private void jButtonSaveMouseClicked(java.awt.event.MouseEvent evt)
    {
        String name = new String(jTextFieldName.getText());
	String action = new String(jTextFieldAction.getText());
        String desctiption = new String(jTextFieldDescription.getText());

	if(name.equals("") || action.equals("") || desctiption.equals(""))
	{
	    jButtonSave.setEnabled(false);
	    JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>Musisz wypełnić wszystkie pola.</FONT></HTML>");
	    JOptionPane.showMessageDialog(null,errorFields);
	    jButtonSave.setEnabled(true);
	    this.setVisible(true);
	}
        else
        {
            try
             {
                 Class.forName("org.postgresql.Driver");
             }
             catch (ClassNotFoundException cnfe)
             {
                 JOptionPane.showMessageDialog(null, cnfe.getMessage());
             }
             try
             {
                 c = DriverManager.getConnection("jdbc:postgresql://localhost/template1",
                                    "postgres", "przemek");

                 PreparedStatement ps = c.prepareStatement("UPDATE actions SET name = ?, description = ?, action = ? WHERE id = ?");
                 ps.setString(1, name);
                 ps.setString(2, desctiption);
                 ps.setString(3, action);
                 ps.setInt(4, actionId);
                 ps.executeUpdate();

                

                 parentFrame.wypelnij_jComboBox();

                 this.dispose();
             }
             catch (SQLException se)
             {
                 JOptionPane.showMessageDialog(null, se.getMessage());
             }
        }


    }

    private void jButtonCancelMouseClicked(java.awt.event.MouseEvent evt)
    {
        this.dispose();
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButtonSave;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldName;
    private javax.swing.JTextField jTextFieldAction;
    private javax.swing.JTextField jTextFieldDescription;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JLabel jLabelAction;
    private javax.swing.JLabel jLabelDescription;
    // End of variables declaration
}

